package uk.co.accolade.clash.app.intellij.clashpanel;

import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.fileEditor.FileEditorManagerEvent;
import com.intellij.openapi.fileEditor.FileEditorManagerListener;
import com.intellij.openapi.vfs.VirtualFile;
import uk.co.accolade.clash.app.ClashPanel;
import uk.co.accolade.clash.app.intellij.IntelliJFileFacade;
import uk.co.accolade.clash.core.ClashFilePath;
import uk.co.accolade.clash.core.ClashHandler;
import uk.co.accolade.clash.core.UserChanges;

import java.util.HashMap;
import java.util.Map;
import java.util.List;
import java.util.Arrays;

public class ClashPanelManager implements ClashHandler, FileEditorManagerListener {

    private IntelliJFileFacade intelliJFileFacade;
    private Map<ClashFilePath, ClashPanel> clashPanels = new HashMap<ClashFilePath, ClashPanel>();

    //todo, we also need to register the ClashPanel as a FileEditorManagerListener


    public ClashPanelManager( IntelliJFileFacade intelliJFileFacade ) {
        this.intelliJFileFacade = intelliJFileFacade;
    }

    public void handleClashes( UserChanges clashedChanges ) {
        for( ClashFilePath filePath : clashedChanges.getChangedFilesReadOnly() ) {
            ClashPanel panel = clashPanels.get( filePath );
            if( panel == null) {
                VirtualFile clashedFile = intelliJFileFacade.virtualFileForPath( filePath );
                if( clashedFile != null )  {
                    panel = new ClashPanel();
                    panel.showClash( clashedChanges.getUsername() );
                    clashPanels.put( filePath, panel );
                    intelliJFileFacade.addClashPanel( clashedFile, panel );
                }
            } else {
                panel.showClash( clashedChanges.getUsername() );
            }
        }
    }

    public void clearClashes( UserChanges clashedChanges ) {
        List<VirtualFile> openFiles = Arrays.asList( intelliJFileFacade.getOpenFiles() );
        for( ClashFilePath filePath : clashedChanges.getChangedFilesReadOnly() ) {
            VirtualFile vFile = intelliJFileFacade.virtualFileForPath( filePath );
            if( openFiles.contains( vFile ) ) {
                ClashPanel panel = clashPanels.get( intelliJFileFacade.getFilename( vFile ) );
                if( panel != null ) {
                    intelliJFileFacade.removeClashPanel( vFile, panel );
                }
            }
        }
        clashPanels = new HashMap<ClashFilePath, ClashPanel>();
    }

    public void fileOpened( FileEditorManager fileEditorManager, VirtualFile virtualFile ) {
        ClashFilePath path = intelliJFileFacade.getFilename( virtualFile );
        ClashPanel panel = clashPanels.get( path );
        if( panel != null ) {
            intelliJFileFacade.addClashPanel( virtualFile, panel );
        }
    }

    public void fileClosed( FileEditorManager fileEditorManager, VirtualFile virtualFile ) {
    }

    public void selectionChanged( FileEditorManagerEvent event ) {
    }
}